package com.kaifamiao.base.Demo210917;

import java.util.Scanner;

/**
 * @author lucien-ma
 * @date 2021/9/17
 * @apiNote
 */
public class Demo01 {

    public static void main(String[] args) {
        String hex = decToAny(input(),16);
        System.out.println(hex);
    }

    public static int input() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个十进制整数：");
        return scanner.nextInt();
    }

    public static String decToAny(int dec, int radix) {
        // 传进来的十进制数
        int decTemp = dec;
        // 转换的进制
        int divisor = radix;
        StringBuilder hex = new StringBuilder();
        do {
            // 余数
            int remainder = decTemp % divisor;
            // 解析余数
            insert(hex, remainder);
            // 计算商
            decTemp = decTemp / divisor;
        } while (decTemp != 0);
        return hex.reverse().toString();
    }

    private static void insert(StringBuilder hex, int remainder) {
        if (remainder >= 0 && remainder <= 9) {
            hex.append(remainder);
        } else {
            switch (remainder) {
                case 10:
                    hex.append("A");
                    break;
                case 11:
                    hex.append("B");
                    break;
                case 12:
                    hex.append("C");
                    break;
                case 13:
                    hex.append("D");
                    break;
                case 14:
                    hex.append("E");
                    break;
                case 15:
                    hex.append("F");
                    break;
            }
        }
    }

}

